package com.example.xloong.mymvvm.model;

import android.util.Log;

import com.example.xloong.mymvvm.base.BaseLoadListener;
import com.example.xloong.mymvvm.bean.NewsBean;
import com.example.xloong.mymvvm.bean.NewsBeanSimple;
import com.example.xloong.mymvvm.http.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Xloong on 2018/1/25.
 */

public class NewsModelImpl implements NewsModel {


    private static final String TAG="NewsModelImpl";
    List<NewsBeanSimple> mList = new ArrayList<>();
    private String thumbiamge;


    @Override
    public void loadNewsData(int page, final BaseLoadListener<NewsBeanSimple> loadListener, String id) {
        HttpUtils.getNewsData(page+"",id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<NewsBean>() {
                    @Override
                    public void onNext(@NonNull NewsBean newsBean) {
                        Log.i(TAG,"onNext--->");
                        List<NewsBean.DataBeanX.ArticleBean.DataBean> mData= newsBean.getData().getArticle().getData();
                        mList.clear();
                        if (mData != null && mData.size()>0){
                            for (NewsBean.DataBeanX.ArticleBean.DataBean mNBean:mData){
                                if (mNBean.getThumb().size()>0){
                                    thumbiamge = mNBean.getThumb().get(0);
                                }else {
                                    thumbiamge = null;
                                }
                                String title = mNBean.getTitle();
                                String descripton = mNBean.getDescription();

                                // 构造Adapter的所需要的数据源

                                NewsBeanSimple newsBeanSimple = new NewsBeanSimple();
                                newsBeanSimple.description.set(descripton);
                                newsBeanSimple.thumbimage.set(thumbiamge);
                                newsBeanSimple.title.set(title);
                                mList.add(newsBeanSimple);
                            }


                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.i(TAG,"onError -->"+e.toString());
                        loadListener.loadFailure(e.toString());

                    }

                    @Override
                    public void onComplete() {
                        loadListener.loadSuccess(mList);
                        loadListener.loadComplete();
                    }

                    @Override
                    protected void onStart() {
                        super.onStart();
                        Log.i(TAG,"onStart -->");
                        loadListener.loadStart();
                    }
                });


    }
}
